package cn.xfdzx.android.apps.shop.fragment.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.XFDGoodsDeatailsActivity;
import cn.xfdzx.android.apps.shop.adapter.MarketGoodsAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.MvpBaseFragment;
import cn.xfdzx.android.apps.shop.bean.FoundGoodContentBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.evenbus.MarketFoundGoodSelectIdMessage;
import cn.xfdzx.android.apps.shop.fragment.home.contract.MarketNearGoodsContract;
import cn.xfdzx.android.apps.shop.fragment.home.presenter.MarketNearGoodsPresenter;
import cn.xfdzx.android.apps.shop.fragment.home.request.NearGoodsRequest;
import cn.xfdzx.android.apps.shop.presenter.MarketBottomPresenter;
import cn.xfdzx.android.apps.shop.util.ItemOffsetDecoration;


public class MarketNearGoodsFragment extends MvpBaseFragment<MarketNearGoodsPresenter> implements MarketNearGoodsContract.View {


    @BindView(R.id.recycle_goods)
    RecyclerView recycleGoods;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private List<FoundGoodContentBeen.DataBean> mGoodList = new ArrayList<>();
    private MarketGoodsAdapter marketGoodsAdapter;

    public static MarketNearGoodsFragment newInstance(String param1) {
        MarketNearGoodsFragment fragment = new MarketNearGoodsFragment();
        Bundle args = new Bundle();
        args.putString("id", param1);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected void initView(View view) {

        EventBus.getDefault().register(this);
        findViewInit();
    }


    private void findViewInit() {
        MarketBottomPresenter mPresenter = new MarketBottomPresenter();

        GridLayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 2);
        recycleGoods.setLayoutManager(mLayoutManager);
        ItemOffsetDecoration itemDecoration = new ItemOffsetDecoration(getContext(), R.dimen.item_offset);
        recycleGoods.addItemDecoration(itemDecoration);
        recycleGoods.setNestedScrollingEnabled(false);
        marketGoodsAdapter = new MarketGoodsAdapter(getActivity(), mGoodList, mPresenter);
        marketGoodsAdapter.setOnItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClick(int position) {

                Intent intent = new Intent(getContext(), XFDGoodsDeatailsActivity.class);
                intent.putExtra("good_id", mGoodList.get(position).getId() + "");
                startActivity(intent);
            }
        });
        recycleGoods.setAdapter(marketGoodsAdapter);

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                initData();
            }
        });

    }

    int page = 1;
    String select_id = "";

    private void initData() {
        NearGoodsRequest goodsRequest = new NearGoodsRequest();
        goodsRequest.setIndex_category_id(select_id);
        goodsRequest.setLimit(Constants.LIMIT);
        goodsRequest.setPages(page);
        mPresenter.getNearGoods(goodsRequest);


    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_market_near_goods;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(Throwable throwable) {
        refreshLayout.finishLoadMore(true);//加载完成

    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    public void getHomeNearGoodsSel(MarketFoundGoodSelectIdMessage messageEvent) {

        mGoodList.clear();
        page = 1;
        select_id = messageEvent.getMessage();
        initData();
        refreshLayout.setNoMoreData(false);
    }

    @Override
    public MarketNearGoodsPresenter createPresenter() {
        return new MarketNearGoodsPresenter();
    }

    @Override
    public void getSuccess(FoundGoodContentBeen foundGoodContentBeen) {

        if (foundGoodContentBeen.getStatus() == Constants.SUCCESS_CODE) {
            mGoodList.addAll(foundGoodContentBeen.getData());
            marketGoodsAdapter.notifyDataSetChanged();

            if (foundGoodContentBeen.getData().size() == 0) {
                refreshLayout.finishLoadMoreWithNoMoreData();
            }
        }
        refreshLayout.finishLoadMore(true);//加载完成

    }

}
